package com.clstu.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketTCP02Client {//注意，双方都在发信息的时候通信需要调用socket的关闭方法
    public static void main(String[] args) throws IOException {

        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);//连接本机的9999端口
        OutputStream outputStream = socket.getOutputStream();
        InputStream inputStream = socket.getInputStream();
        int readLen = 0;
        byte[] buf = new byte[1024];
        outputStream.write("hello , server !".getBytes());
        socket.shutdownOutput();//设置结束语标志(表示自己说话已经说完了），防止两方一直等着

        while ((readLen = inputStream.read(buf))!=-1){
            System.out.println(new String(buf,0,readLen));
        }
        inputStream.close();
        outputStream.close();
        socket.close();
    }
}
